<?php
class PM_g
{
    public static function __index( $user_id , $room_param , $db_connect ){
        //查询用户是否存在以及获取用户信息
        $user_info = PHZ_Member::__MemberISin_AND_getInfo( $user_id , $db_connect );
        if( $user_info == false ){
            return array('status'=> 0 ,'msg'=>'用户信息错误,无法创建房间');
        }
        //拆开房间参数信息,分别进行局数，玩法，代开判断
        $room_param_info = explode("|", $room_param );
        if( $room_param_info[0] == "" || $room_param_info[1] == "" || $room_param_info[2] == "" ){
            return array('status'=> 1 ,'msg'=>'房间信息提交有误,无法创建房间');
        }
        //判断当前用户是否在游戏状态? - 代开的情况下不需要判断
        //这里不需要判断是否已经存在开房的确没有开始的，因为代开不会自动进房；非代开自动进房，也过不了这个判断
        if( $room_param_info[0] == "0" ) {
            $is_gameIng = PHZ_Member::__getMemberISGameing($user_id, $db_connect);
            if ($is_gameIng['status'] != false) {
                return array('status' => 2 , 'msg' => '当前用户正处于游戏状态,无法创建房间', 'room_id' => $is_gameIng['room_id'] , 'max_id' => $is_gameIng['max_id'] );
            }
        }
        //执行局数操作
        if( $room_param_info[1] != "1" && $room_param_info[1] != "5" && $room_param_info[1] != "8" && $room_param_info[1] != "16" ){
            return array('status'=> 3 ,'msg'=>'房间信息提交有误,无法创建房间');
        }
        $z_js    = $room_param_info[1];
        switch ( $z_js ){
            case 1:$consume  = JS_1;break;
            case 5:$consume  = JS_5;break;
            case 8:$consume  = JS_8;break;
            case 16:$consume = JS_16;break;
        }
        //如果当前是代理，则把需要消耗的钻石调为0
        if( $user_info['level'] >= 1 ){
            $consume = 0;
        }
        //当本次开房需要消耗钻石的时候，且代理等于是0的时候，需要进行余额判断
        if( $consume >= 1){
            if( $user_info['score'] < $consume ){return array('status'=> 4 ,'msg'=>'钻石余额不足');}
        }
        //进行玩法操作
        $room_param_info[2] = substr($room_param_info[2],0,-1);
        //进行房间添加操作(这里先扣除，后添加) - 免费场的情况下，不需要进行扣除
        if( $consume >= 1) {
            $score = $user_info['score'] - $consume;
            $deduction_exe = $db_connect->update('tt_member')->cols(array('score' => $score))->where('uid=' . $user_id)->query();
            if ($deduction_exe != 1) {
                if ($user_info['score'] < $consume) {
                    return array('status' => 5 , 'msg' => '钻石余额不足');
                }
            }
        }
        /******************************************  数据  ************************************/
        $data = array();
        $data['room_id']     = PHZ_Room::__getRandRoomID( $db_connect );
        $data['room_cid']    = $user_id;
        $data['room_status'] = 0;
        $data['room_js']     = 1;
        $data['room_zjs']    = $z_js;
        $data['room_difen']  = 2;
        $data['room_tp']     = 1;//固定为1，六胡抢
        $data['room_mx']     = 3;//六胡抢固定为3个人 - 默认为3个人
        $data['room_qh']     = 6;//六胡抢起息为6胡息
        $data['room_mtype']      = $room_param_info[2];//用下划线隔开的玩法
        $data['room_time']       = date('Y-m-d H:i:s',time());
        $data['room_time_micor'] = time();
        $data['consume']         = $consume;
        $data['room_c_level']    = $user_info['level'];//记录开房者当时的代理等级
        if( $room_param_info[0] == "1" ){//是否为代开，非代开开房玩家直接为庄且占据第一个位置
            $data['is_daikai'] = 1;
        }else{
            $data['is_daikai'] = 0;
            $data['banker']    = $user_id;
            $data['room_player_1'] = $user_id;
        }
        $db_connect->insert('tt_room')->cols( $data )->query();//新增房间
        $room_info = PHZ_Room::__getRoomInfo_byID( $data['room_id'] , $db_connect );
        if( $room_info == null || $room_info == false || $room_info == "" ){
            return array('status' => 3,'msg'=>'房间创建失败,请稍后重试');
        }
        /******************************************  数据  ************************************/
        //更新开房者的开房方式记录
        PHZ_Member::__updateHis_CJ( $user_id , $room_param , $db_connect );
        //创建房间成功后，更新房间缓存信息
        PHZ_Memcache::__set_RoomInfo_ToMemcache( $data );
        return array( 'status' => 8 , 'msg'=> '房间信息创建成功' , 'data' => $data );
    }
}